<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Application
 *
 * Application specific setter/getter, esp. name and group
 */

namespace crcl\base;

interface IApplication
{
  /**
   * Sets vendor, project, subproject
   * used on for srv directory (cache, log, data)
   *
   * @static
   *
   * @param string  $sVendor      vendor or customer like "crcl"
   * @param string  $sProject     project name like "base"
   * @param string  $sSubProject  like "frontend"
   */
  public static function setIdentifier($sVendor, $sProject, $sSubProject);

  /**
   * Returns identifier string (vendor/project/subproject)
   *
   * @return string Identifier
   */
  public static function getIdentifier();

  /**
   * Returns group (vendor/project), @see setIdentifier
   *
   * @static
   * @return  string
   */
  public static function getGroup();

  /**
   * Returns SubProject, @see setIdentifier
   *
   * @static
   * @return  string
   */
  public static function getSubProject();

  /**
   * Sets SubProject
   *
   * @static
   *
   * @param string  $sSubProject Subproject
   */
  public static function setSubProject($sSubProject);

  /**
   * Returns Project, @see setIdentifier
   *
   * @static
   * @return  string
   */
  public static function getProject();

  /**
   * Returns Vendor, @see setIdentifier
   *
   * @static
   * @return  string
   **/
  public static function getVendor();

  /**
   * Tests if identifier is set
   *
   * @throws Exception
   */
  public static function testIdentifierSet();
}

class Application implements IApplication
{
  public static function setIdentifier($sVendor, $sProject, $sSubProject)
  {
    //NULL is allowed - to remove identifier
    if (($sVendor === '') || ($sProject === '') || ($sSubProject === ''))
    {
      throw new application\Exception(
        'Invalid identifier',
        application\Exception::ERROR_INVALIDIDENTIFIER,
        array(
             'vendor'     => $sVendor,
             'project'    => $sProject,
             'subproject' => $sSubProject));
    }
    self::$sVendor = $sVendor;
    self::$sProject = $sProject;
    self::$sSubProject = $sSubProject;

    application\Path::resetCache();
  }

  public static function getGroup()
  {
    $sGroup = self::$sVendor . '/' . self::$sProject;
    return $sGroup;
  }

  public static function getVendor()
  {
    return self::$sVendor;
  }

  public static function getProject()
  {
    return self::$sProject;
  }

  public static function  getSubProject()
  {
    return self::$sSubProject;
  }

  public static function setSubProject($sSubProject)
  {
    self::$sSubProject = $sSubProject;
  }

  public static function getIdentifier()
  {
    self::testIdentifierSet();

    $sIdentifier = self::$sVendor . '/' .
                   self::$sProject . '/' .
                   self::$sSubProject;

    return $sIdentifier;
  }

  public static function testIdentifierSet()
  {
    if ((\is_null(self::$sVendor)) ||
        (\is_null(self::$sProject)) ||
        (\is_null(self::$sSubProject))
    )
    {
      throw new application\Exception(
        'No identifier',
        application\Exception::ERROR_NOIDENTIFIER,
        array('vendor' => self::$sVendor,
              'project' => self::$sProject,
              'subproject' => self::$sSubProject));
    }
  }

  /** PRIVATE *************************************************************************************/

  private static $sVendor = \NULL;
  private static $sProject = \NULL;
  private static $sSubProject = \NULL;
}